package jman.servlet;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import jman.service.UserService;

/**
 * Servlet implementation class Regist
 */
public class Regist extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Regist() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String name=(String) request.getParameter("name");
		String pwd=(String) request.getParameter("pwd");
		String role=(String) request.getParameter("role");
		int id=0;
		boolean flag=false;
		UserService us=new UserService();
		flag=us.regist(name, pwd, role);
		try {
			id=us.tellIdToUser(name, pwd, role);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if(flag){
			request.setAttribute("id",id);
			request.getRequestDispatcher("/jman/admin/registsuccess.jsp").forward(request, response);
		}else{
			request.getRequestDispatcher("/jman/admin/registfail.html").forward(request, response);
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
